﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
namespace Z2.Core
{

	#region Using

	using System;
	using Microsoft.Xna.Framework;
	using System.Collections.Generic;

	#endregion

	/// <summary>
	/// Chování objektu typu "ohyb v ose Y"
	/// </summary>
	internal class BendableBehavior : ObjectBehaviorBase
	{

		#region Members

		private int m_bend;

		#endregion

		#region Constructor

		/// <summary>
		/// Konstruktor
		/// </summary>
		public BendableBehavior()
			: base()
		{
		}

		#endregion

		#region Properties

		/// <summary>
		/// Nastaví nebo vrací ohyb
		/// </summary>
		public int Bend
		{
			get
			{
				return this.m_bend;
			}
			set
			{
				this.m_bend = value;
			}
		}

		#endregion

		#region Override Methods

		/// <summary>
		/// Metoda, která modifikuje vektor pohybu objektu <paramref name="obj"/>
		/// </summary>
		/// <param name="obj"><see cref="ObjectBase"/></param>
		internal override void ModifyVector(ObjectBase obj)
		{
			Rectangle rect = obj.GetRectangle(0, -WorldField.SIZE);
			List<ObjectBase> objects;
			IntersectionResult result;

			if (obj.Move.Y > 0)
			{
				//objects = World.Current.GetSectorsObjects(obj);
				//bool collision = false;
				//foreach (ObjectBase objBase in objects)
				//{
				//    if (obj == objBase)
				//        continue;
				//    result = obj.Intersects(objBase, rect);
				//    if (result.IsCollision)
				//    {
				//        collision = true;
				//    }
				//}
				//if (!collision)
				//    return;

				if (Math.Abs(obj.Position.Y - obj.InitialPosition.Y) < this.m_bend)
				{
					obj.Position = new Vector2(obj.Position.X, obj.Position.Y + obj.Move.Y);
				}
				else
				{
					obj.Move = new Vector2(obj.Move.X, -obj.Move.Y);
				}
			}
			else
			{
				objects = World.Current.GetSectorsObjects(obj);
				foreach (ObjectBase objBase in objects)
				{
					if (obj == objBase)
						continue;
					result = obj.Intersects(objBase, rect);
					if (result.IsCollision)
					{
						return;
					}
				}

				if (obj.Position.Y != obj.InitialPosition.Y)
				{
					obj.Position = new Vector2(obj.Position.X, obj.Position.Y + obj.Move.Y);
				}
			}
		}

		#endregion

	}
}
